//
// Created by Administrator on 2019/12/10.
//
#include <iostream>
#include <cstring>
#include <string>
#include <time.h>


using namespace std;

#define WIDTH 5
#define LENGTH 8
#define HEIGHT 10


/**
 * 取最大值
 * @param a 数字a
 * @param b 数字b
 * @return 返回最大值
 */
int max(int a, int b) {
    int temp;
    if (a > b) {
        temp = a;
    } else {
        temp = b;
    }
    return temp;
}

/**
 * 获取随机数<br/>
 * 注：r[10]必须为static
 * @return
 */
int * getRandom(){
    static int r[10];
    srand(time(NULL));
    cout << "设置数组中10个随机值：";
    for (int i = 0; i < 10; ++i) {
        r[i] = rand();
        cout << r[i] << ",";
    }
    cout << endl;
    return r;
}

/**
 * 设置指针par为当前时间戳
 * @param par
 */
void getSecond(long *par) {
    *par = time(NULL);
}


/**
 *  直接打印
 * @return
 */
void print1() {
    cout << "执行方法print1================================" << endl;
    std::cout << "Hello, World!" << std::endl;
}

/**
 * 加减乘除法
 * @return
 */
void print2() {
    cout << "执行方法print2================================" << endl;
    int x, y = 6;
    x = y;
    cout << "x = " << x << "; y = " << y << endl;
    cout << "x + y = " << x + y << "; x * y = " << x * y << endl;
    cout << "x - y = " << x - y << "; x / y = " << x / y << endl;
    cout << "x % y = " << x % y << "; x + y * 10 + 3 = " << x + y * 10 + 3 << endl;     // 此处得到 69数据，说明加减法顺序是遵循先乘除后加减的
//    char[] sss = {"我的测试数据"};

}

/**
 * 常量 长宽高得到体积
 * @return
 */
void print3() {
    cout << "执行方法print3================================" << endl;
    int area = LENGTH * WIDTH;
    const char NEW_LINE = '\n';     // 常量换行


    cout << "长宽高之和得到体积" << LENGTH * WIDTH * HEIGHT << endl;
    cout << "得到面积" << area << endl;
    cout << "换行测试" << NEW_LINE;
    cout << "最后结尾";

}

/**
 * 循环if等打印,for循环，switch，调用函数
 * @return
 */
void print4() {
    cout << "执行方法print4================================" << endl;
    for (int i = 0; i < 100; ++i) {
        if (i % 6 == 0) {
            cout << "if判断%6取模 == 0--" << i << endl;
        }
        switch (i) {
            case 7:
                cout << "如果i == 7的时候就打印 --switch--" << i << endl;
                break;
            case 9 :
                cout << "如果i == 9的时候就打印 --switch--" << i << endl;
                break;
            default :
                cout << "其他情况" << i << endl;
                break;
        }
    }

    int a = 67, b = 323;
    cout << "数字" << a << "," << b << "--返回最大值--" << max(a, b) << endl;


}

/**
 * char 字符串相关操作
 * @return
 */
void print5() {
    cout << "执行方法print5================================" << endl;
    char hello[] = "hello";
    char world[] = "world";
    char str[11];

    strcpy(str, world);

    cout << str << endl;

    cout << "hello-->" << hello << ", world -->" << world << endl;

    strcat(hello, world);

    cout << "链接hello world之后，再执行：hello-->" << hello << ", world -->" << world << endl;

    try {
        strcat(hello, world);
        strcat(hello, world);
        cout << "链接hello world之后，再执行：hello-->" << hello << ", world -->" << world << endl;
    } catch (exception e) {
        cout << e.what() << endl;
    }
    int helloStr = strlen(hello);
    cout << "hello现在的长度是多少-->" << helloStr << endl;


    string helloString = "hello string类";
    string worldString = "world string类";

    cout << "string类的使用-->helloString--" << helloString << ", worldString--" << worldString
         << ", worldString max_size长度--" << worldString.max_size() << ", worldString size--" << worldString
                 .size() << endl;

    cout << worldString.length() << "--" << endl;

}

/**
 * 指针测试
 * @return
 */
void print6() {
    cout << "执行方法print6================================" << endl;
    int var1;
    char var2[10];
    cout << "打印var1,var2地址" <<endl;
    cout << "var1地址为--" << &var1 << endl;
    cout << "var2地址为--" << &var2 << endl;
    int num = 20;
    int *p ;
    p = &num;
    cout << "int类型num数值为：" << num << ",地址为:" << &num << endl;
    cout << "p指针的值为：" << p << ",地址为：" << &p << "指针的所指向的值为：" << *p << endl;

    // 获取随机数，返回指针：注意返回的指针必须声明为static
    int *pRand;
    pRand = getRandom();
    cout << "打印出指针获取的值:" ;

    for (int i = 0; i < 10; ++i) {
        cout << *(pRand + i) << "," ;
    }
    cout << endl;

    // 传递指针类型到函数中
    long par;
    getSecond(&par);
    cout << "当前时间戳:" << par << endl;
}

// 时间相关
void print7(){
    time_t t = time(0);
    char* dt = ctime(&t);
    cout << "本地时间:" << dt << endl;

    // 把时间更新为tm格式
    tm *tt = gmtime(&t);

    char *dtt = asctime(tt);
    cout << "UTC时间格式:" << dtt << endl;

    cout << "1970年到现在的秒数:" << t << endl;

    // 输出tm各个格式的内容
    tm *tm1 = localtime(&t);

    cout << "年:" << tm1->tm_year + 1900 << endl;
    cout << "月:" << tm1->tm_mon << endl;
    cout << "日:" << tm1->tm_mday << endl;
    cout << "时:" << tm1->tm_hour << endl;
    cout << "分:" << tm1->tm_min << endl;
    cout << "秒:" << tm1->tm_sec << endl;

}

// 基本输入输出
void print8(){
//    char str[] = "unable to read ...";
    // 日志，非缓冲区
//    cerr << "cerr error message :" << str << endl;
    // 日志，缓冲区
//    clog << "clog error message :" << str << endl;
    time_t tt = time(0);
    tm *tm1 = localtime(&tt);


    char name[50];
    int year, month, day;
    cout << "请输入您的姓名：" << endl;
    cin >> name;
    cout << "您输入的姓名为：" << name << endl;
    cout << "请分别输入您的出生年、月、日" << endl;
    cin >> year >> month >> day;
    cout << "您的出生日期为：" << year << "年" << month << "月" << day << "日" << endl;

    cout << "您的岁数为：" << tm1->tm_year + 1900 - year + 1 <<endl;



}

class Box {
public:
    int length;
    Box(){
        this->length = 0;
    }

    Box(int length) {
        this->length = length;
    }

    Box operator+(const Box &b) {
        Box box;
        box.length = this->length + b.length;
        return box;
    }
};

/**
 * 运算符重载
 */
void print9(){
    Box *b1 = new Box(5);
    Box *b2 = new Box(10);
    Box b3 = *b1 + *b2;
    cout << b3.length << endl;
}